<?php
/**
 * Search.php
 * Created by PhpStorm.
 * User: 江小波
 * Date: 2019/06/12
 * Time: 13:27
 */
namespace app\api\open\region;

use app\api\open\Base;
use app\api\model\RegionModel;

class Search extends Base
{
    protected $where = [];

    public function __construct($param = [])
    {
        parent::__construct($param);

		// 是否启用（0否，1是）
		if(isset($this->params['is_enable']) && $this->params['is_enable']!='')
		{
			$this->where['is_enable'] = $this->params['is_enable'];
		}
		// 排序
		if(isset($this->params['sort']) && $this->params['sort']!='')
		{
			$this->where['sort'] = $this->params['sort'];
		}
		// 级别类型（1:一级[所有省], 2：二级[所有市], 3:三级[所有区县], 4:街道[所有街道]）
		if(isset($this->params['level']) && $this->params['level']!='')
		{
			$this->where['level'] = $this->params['level'];
		}
		// 父id
		if(isset($this->params['pid']) && $this->params['pid']!='')
		{
			$this->where['pid'] = $this->params['pid'];
		}
		// 城市首字母
		if(isset($this->params['letters']) && $this->params['letters']!='')
		{
			$this->where['letters'] = $this->params['letters'];
		}
		// 名称
		if(isset($this->params['name']) && $this->params['name']!='')
		{
			$this->where['name'] = $this->params['name'];
		}
    }
    
    /**
     * @param array $param
     * @return array|false|null|\PDOStatement|string|\think\Model
     * @throws \Exception
     */
    public function index($param=[])
    {
        // 获取列表
        return RegionModel::getInstance()->getList($this->where,[
            'field'=> '*'
        ]);
    }

    /**
     * @param array $param
     * @return array|false|\PDOStatement|string|\think\Collection
     */
    public function all($param=[])
    {
        // 获取列表
        return RegionModel::getInstance()->getAll($this->where,[
            'field'=> 'id,name'
        ]);
    }
}